<template>
    <div v-if="is_shown" id="app">
        <hb-head headfont="用户设置"></hb-head>
        <div class="mui-content">
            <ul class="mui-table-view">
                <li class="mui-table-view-cell mui-media">
                    <a class="mui-navigate-right">
                        <img class="mui-media-object mui-pull-left" src="http://www.gzfhxxkj.com/admin/static/dist/img/avatar5.png">
                        <div class="mui-media-body">
                            {{user.username}}
                            <p class="mui-ellipsis">{{level.name}}</p>
                        </div>
                    </a>
                </li>
            </ul>
            <ul class="mui-table-view">
                <li class="mui-table-view-cell mui-media" style="text-align: left;" @tap.prevent.stop="openPanel(0)">
                    <a href="#" class="mui-navigate-right">
                        <img class="slg mui-media-object mui-pull-left" src="../../assets/img/order.png">
                        <div class="mui-media-body">
                            我的订单
                        </div>
                        <span class="text">全部订单</span>
                    </a>
                </li>
            </ul>
            <div class="mui-card">
                <div class="mui-card-content">
                    <div class="content-banner">
                        <div class="img-button" @tap.prevent.stop="openPanel(1)">
                            <span class="button-header">
                                <span v-if="to_pay > 0" class="mui-badge mui-badge-danger">{{to_pay}}</span>
                                <img src="../../assets/img/card.png">
                            </span>
                            <span class="button-footer">待付款</span>
                        </div>
                        <div class="img-button" @tap.prevent.stop="openPanel(3)">
                            <span class="button-header">
                                <span v-if="to_recieve > 0" class="mui-badge mui-badge-danger">{{to_recieve}}</span>
                                <img src="../../assets/img/truck.png">
                            </span>
                            <span class="button-footer">待收货</span>
                        </div>
                        <div class="img-button" @tap.prevent.stop="openPanel(5)">
                            <span class="button-header">
                                <img src="../../assets/img/wrench.png">
                            </span>
                            <span class="button-footer">退还/售后</span>
                        </div>
                    </div>
                </div>
            </div>
            
            <ul class="mui-table-view">
                <li class="mui-table-view-cell mui-media" style="text-align: left;">
                    <a href="#" class="mui-navigate-right">
                        <img class="slg mui-media-object mui-pull-left" src="../../assets/img/money.png">
                        <div class="mui-media-body">
                            我的钱包
                        </div>
                    </a>
                </li>
            </ul>
            <div class="mui-card">
                <div class="mui-card-content">
                    <div class="content-banner">
                        <div class="text-button" @tap.prevent.stop="openRecharge()">
                            <span class="header-text">{{user.money / 100}}</span>
                            <span>账户余额</span>
                        </div>
                        <div class="text-button" @tap.prevent.stop="openScoreRecord()">
                            <span class="header-text">{{user.score / 100}}</span>
                            <span>积分</span>
                        </div>
                        <div class="text-button">
                            <span class="header-text">{{collection_count}}</span>
                            <span>收藏</span>
                        </div>
                        <div class="text-button">
                            <span class="header-text">{{msg_count}}</span>
                            <span>消息</span>
                        </div>
                    </div>
                </div>
            </div>
    
            <ul class="slg mui-table-view mui-table-view-chevron">
                <li class="mui-table-view-cell mui-media" style="text-align: left;">
                    <a href="#" class="mui-navigate-right">
                        <img class="slg mui-media-object mui-pull-left" src="../../assets/img/share.png">
                        <div class="mui-media-body">
                            分享有奖
                        </div>
                    </a>
                </li>
                <li class="mui-table-view-cell mui-media" style="text-align: left;">
                    <a href="#" class="mui-navigate-right">
                        <img class="slg mui-media-object mui-pull-left" src="../../assets/img/user_service.png">
                        <div class="mui-media-body">
                            客户服务
                        </div>
                    </a>
                </li>
                <li class="mui-table-view-cell mui-media" style="text-align: left;">
                    <a href="#" class="mui-navigate-right">
                        <img class="slg mui-media-object mui-pull-left" src="../../assets/img/about.png">
                        <div class="mui-media-body">
                            关于我们
                        </div>
                    </a>
                </li>
            </ul>
    
            <ul class="mui-table-view">
                <li class="mui-table-view-cell" style="text-align: center;" @click="logout">
                    <a>退出登录</a>
                </li>
            </ul>
        </div>
         <order-list :activeCategory="activeCategory"></order-list> 
    </div>
</template>

<script>

import { ajax, userSettings, userRedirect } from 'assets/js/Lib'

import 'assets/css/mui.css'

import HbHead from 'components/HbHead'
import NavBarTab from 'components/NavBarTab'
import OrderList from 'components/OrderList'

export default {
    data() {
        return {
            user: {},
            level: {},
            to_pay: 0,
            to_recieve: 0,
            collection_count: 0,
            msg_count: 0,
            activeCategory: 1,
            is_shown: false,
        }
    },
    components: {
        HbHead, NavBarTab, OrderList
    },
    computed: {

    },
    //实例初始化最之前，无法获取到data里的数据
    beforeCreate() {

    },
    //在挂载开始之前被调用
    beforeMount() {

    },
    //已成功挂载，相当ready()
    mounted() {

        localStorage.setItem('$needloginpage', JSON.stringify({
            'id': 'user',
            'url': 'user.html'
        }))

        const vm = this;

        let cache = localStorage.getItem('$userData')

        if(cache) {
            cache = JSON.parse(cache)
            vm.user = cache.user
            vm.level = cache.user.level
            vm.to_pay = cache.to_pay
            vm.to_recieve = cache.to_recieve
            vm.msg_count = cache.msg_count
        }

        ajax({
            url: '/api/user/index',
            type: 'GET',
            success: (res) => {
                localStorage.setItem('$userData', JSON.stringify(res))
                vm.user = res.user
                vm.level = res.user.level
                vm.to_pay = res.to_pay
                vm.to_recieve = res.to_recieve
                vm.msg_count = res.msg_count

              ajax({
                url: '/api/public/oauth_login',
                type: 'GET',
                data: {url: window.location.href},
                success: (res) => {
                  console.log('oauth_login', res);
                  if (res.url) {
                    window.location.href = res.url;
                  } else {
                    this.is_shown = true;
                  }
                }
              });
            }
        })

        mui.init({
            statusBarBackground: '#f7f7f7'
        })
    },
    //相关操作事件
    methods: {
        logout: function () {
            let setting = userSettings.getSettings()
            delete setting.token
            userSettings.setSettings(setting)

            window.toPage({
                "id": 'scorehome',
                "url": 'scorehome.html'
            });
        },
        openPanel: function (index) {
            this.activeCategory = index
            mui('.slg-product-list').popover('show')
        },
        openRecharge: function () {
            window.toPage({
                "id": 'recharge',
                "url": 'recharge.html'
            });
        },
        openScoreRecord: function () {
            window.toPage({
                "id": 'scorerecord',
                "url": 'scorerecord.html'
            });
        },
    }
}
</script>

<style lang="less">
.content-banner {
    height: 60px;
}

.text-button {
    width: 24.99%;
    height: inherit;
    float: left;
    padding: 15px;
    cursor: pointer;

    &:active {
        background: #eee;
    }

    span {
        display: block;
        font-size: 12px;
        line-height: 12px;
        color: #232326;
        text-align: center;
    }

    span.header-text {
        font-size: 16px;
        line-height: 16px;
        margin-bottom: 6px;
    }
}

.img-button {
    width: 33.33%;
    height: inherit;
    float: left;
    padding: 15px;
    cursor: pointer;

    &:active {
        background: #eee;
    }

    img {
        width: 100%;
        height: 100%;
    }

    span.button-header {
        width: 21px;
        height: 18px;
        margin: 0 auto;
        margin-bottom: 4px;
        position: relative;
        display: block;

        .mui-badge {
            position: absolute;
            right: -15px;
            top: -10px;
        }
    }

    span.button-footer {
        display: block;
        font-size: 11px;
        line-height: 11px;
        color: #232326;
        text-align: center;
        margin-bottom: 4px;
    }
}

#app {

    img.slg {
        width: 15px;
        height: 18px;
    }

    .mui-table-view~.mui-card {
        margin-top: -2px;
    }

    .mui-table-view-cell {
        &:after {
            left: 0px;
        }
    }

    .mui-navigate-right {
        padding: 10px 65px 10px 11px;
        font-size: 14px;
        min-height: 32px;
    }

    .mui-navigate-right>.text {
        color: #bbb;
        right: 35px;
        position: absolute;
        top: 28%;
    }

    .mui-card {
        margin: 15px 0 10px 0;
        border-radius: 0px;
        box-shadow: 0 0px 1px rgba(0, 0, 0, .3);
    }

    .mui-table-view {
        margin-top: 25px;
    }
}
</style>
